package com.bilibili.service.impl;

import com.bilibili.common.BaseEnum;
import com.bilibili.entity.UserFollowingGroup;
import com.bilibili.enumeration.GroupType;
import com.bilibili.mapper.UserFollowingGroupMapper;
import com.bilibili.service.UserFollowingGroupService;
import io.mybatis.service.AbstractService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class UserFollowingGroupServiceImpl extends AbstractService<UserFollowingGroup, Long, UserFollowingGroupMapper> implements UserFollowingGroupService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void createGroup(Long userId, String name) {
//        GroupType groupType = BaseEnum.getByCode(type, GroupType.class);
//        if(!GroupType.CUSTOM.equals(groupType)){
//            throw new RuntimeException("无权创建分组类型");
//        }
        Optional<UserFollowingGroup> userFollowingGroupOptional = this.baseMapper.wrapper().eq(UserFollowingGroup::getUserId, userId).eq(UserFollowingGroup::getName, name).one();
        if (userFollowingGroupOptional.isPresent()) {
            throw new RuntimeException("分组名已存在");
        }
        UserFollowingGroup userFollowingGroup = new UserFollowingGroup();
        userFollowingGroup.setUserId(userId);
        userFollowingGroup.setName(name);
        userFollowingGroup.setType(GroupType.CUSTOM.getCode());
        Date now = new Date();
        userFollowingGroup.setCreateTime(now);
        userFollowingGroup.setUpdateTime(now);
        this.baseMapper.insert(userFollowingGroup);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleterGroup(Long groupId) {
        // TODO: 2024/2/7 获取当前用户ID
        Optional<UserFollowingGroup> userFollowingGroupOptional = this.baseMapper.wrapper().eq(UserFollowingGroup::getId, groupId).eq(UserFollowingGroup::getUserId, 1L).one();
        userFollowingGroupOptional.ifPresent(group -> this.baseMapper.deleteByPrimaryKey(groupId));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateGroupName(Long groupId, String groupName) {
        Long userId = 1L;
        Optional<UserFollowingGroup> userFollowingGroupOptional = this.baseMapper.wrapper().eq(UserFollowingGroup::getUserId, userId).eq(UserFollowingGroup::getName, groupName).one();
        if (userFollowingGroupOptional.isPresent()) {
            throw new RuntimeException("分组名已存在");
        }
        UserFollowingGroup userFollowingGroup = new UserFollowingGroup();
        userFollowingGroup.setId(groupId);
        userFollowingGroup.setName(groupName);
        userFollowingGroup.setUpdateTime(new Date());
        this.baseMapper.updateByPrimaryKeySelective(userFollowingGroup);
    }

    @Override
    public List<UserFollowingGroup> queryListByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new RuntimeException("分组ID为空");
        }
        return this.baseMapper.selectByFieldList(UserFollowingGroup::getId, ids);
    }
}
